package com.kami.ip;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

import com.kami.ip.view.activity.checkMain;

//mainActivity 继承自AppConpatActivity类，并要实现导航栏监听接口，该接口只有一个要实现的方法返回布尔值 ，和view点击监听接口
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    ImageButton bmi,calendar,footCounter,checkupHistory; //声明控件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar =  findViewById(R.id.toolbar);  //找到头部的导航按钮
        setSupportActionBar(toolbar);     //设置顶部导航按钮

        FloatingActionButton fab =  findViewById(R.id.fab);  //找到底部浮动按钮
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer =  findViewById(R.id.drawer_layout);   //找到导航总布局包括左侧导航栏和主页面，重合的布局
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);//监听toogle触发器
        toggle.syncState();     //同步状态

        NavigationView navigationView =  findViewById(R.id.nav_view);  //找到导航布局
        navigationView.setNavigationItemSelectedListener(this);           //监听导航栏布局选项点击监听器

        bmi = findViewById(R.id.bmi);
        calendar = findViewById(R.id.calendar);
        footCounter = findViewById(R.id.footCounter);
        checkupHistory = findViewById(R.id.checkupHistory);
        bmi.setOnClickListener(this);
        calendar.setOnClickListener(this);
        footCounter.setOnClickListener(this);
        checkupHistory.setOnClickListener(this);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer =  findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
    //process the click event for bmi,calendar,footcounter,checkuphistory
        switch (v.getId()){
            case R.id.bmi:
                intent.setClass(getApplicationContext(), bmi.class);
                startActivity(intent);
                break;
                //未完待续，此处填写主页面另外三个图片按钮模块的点击事件
            case R.id.checkupHistory:
                intent.setClass(getApplicationContext(), checkMain.class);
                startActivity(intent);
            default:
                break;
        }

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        return false;
    }
}
